/**
 * Created by XiaAo on 2017/5/8.
 */

// //student：获取或者创建一个数据库，如果数据库不存在那么创建之
// var dataBase = openDatabase("", "1.0", "学生表", 1024 * 1024, function () { });
function initDatabase() {
    var db = getCurrentDb();//初始化数据库
    console.log();
    if (!db) {
        alert("您的浏览器不支持HTML5本地数据库");
        return;
    }
    db.transaction(function (trans) {//启动一个事务，并设置回调函数
            //执行创建表的Sql脚本

            trans.executeSql("CREATE TABLE IF NOT EXISTS student(sId SMALLINT  NULL, sName TEXT NULL,sSex TEXT NULL,sPaper TEXT NULL,sPatent TEXT NULL,sResearch TEXT NULL,sReward TEXT NULL,sSession TEXT NULL)", [],
                function (trans, result) {
                },
                function (trans, message) {
                    alert(message);
                });
        },
        function (trans, result) {
        },
        function (trans, message) {
        });
}

$(function () {//页面加载完成后绑定页面按钮的点击事件
    initDatabase();
    //增加
    $("#btnSave").click(function () {
        var sId = $("#sId").val();
        var sName = $("#sName").val();
        var sSex = $("#sSex").val();
        var sPaper = $("#sPaper").val();
        var db = getCurrentDb();
        //执行sql脚本，插入数据
        db.transaction(function (trans) {
            trans.executeSql("INSERT INTO student(sId,sName,sSex,sPaper) VALUES(?,?,?,?) ",
                [sId, sName, sSex, sPaper], function (ts, data) {
                }, function (ts, message) {
                    console.log(ts);
                    alert(message);
                });
        });
        showAllTheData();
    });

    //查询
    $("#btnQuery").click(function () {
        var sId = $("#sId").val();
        var sName = $("#sName").val();
        var sSex = $("#sSex").val();
        var sPaper = $("#sPaper").val();
        var db = getCurrentDb();
        //执行sql脚本，插入数据
        var selectSQL = 'SELECT * FROM  student  WHERE sName=?';
        db.transaction(function (trans) {
            trans.executeSql(selectSQL, [sName],
                function (tx, result) {
                    alert('查询成功: ' + result.rows.length);
                    var len = result.rows.length;
                    for (var i = 0; i < len; i++) {
                        sId.value = result.rows.item(i).sId;
                        sName.innerHTML = result.rows.item(i).sName;
                        sSex.innerHTML = result.rows.item(i).sSex;
                        sPaper.innerHTML = result.rows.item(i).sPaper;
                        console.log(len);
                        console.log("sName = " + result.rows.item(i).sName);
                        console.log("sSex = " + result.rows.item(i).sSex);
                        console.log("sPaper = " + result.rows.item(i).sPaper);
                    }
                },
                function (tx, error) {
                    alert('查询失败: ' + error.message);
                });
        });
        showAllTheData();
    });

    //修改更新
    // $("#btnDelete").click(function () {
    //     var sId = $("#sId").val();
    //     var sName = $("#sName").val();
    //     var sSex = $("#sSex").val();
    //     var sPaper = $("#sPaper").val();
    //     //执行sql脚本，插入数据
    //     db.transaction(function (trans) {
    //         trans.executeSql("DELETE FROM student WHERE sId=?", [id], function (ts, res) {
    //             alert('删除' + id + '成功！！');
    //         }, function (trans, err) {
    //             alert('删除失败' + err.message)
    //         });
    //
    //     });
    //     showAllTheData();
    // })

    //删除
    $("#btnDelete").click(function () {
        var id = $("#sId").val();
        var name = $("#sName").val();
        var db = getCurrentDb();
        //执行sql脚本，插入数据
        // db.transaction(function (trans) {
        //     trans.executeSql('DELETE FROM student');
        // })
        db.transaction(function (trans) {
            trans.executeSql("DELETE FROM student WHERE sName=?", [name], function (ts, res) {
                alert('删除' + name + '成功！！');
            }, function (trans, err) {
                alert('删除失败' + err.message)
            });

        });
        showAllTheData();
    })

});
function getCurrentDb() {
    //打开数据库，或者直接连接数据库参数：数据库名称，版本，概述，大小
    //如果数据库不存在那么创建之
    var db = openDatabase("myDb", "1.0", "it's to save student data!", 1024 * 1024);
    return db;
}

//显示所有数据库中的数据到页面上去
function showAllTheData() {
    $("#tblData").empty();
    var db = getCurrentDb();
    db.transaction(function (trans) {
        trans.executeSql("SELECT * FROM student ", [], function (ts, data) {
            console.log(data);
            if (data) {
                for (var i = 0; i < data.rows.length; i++) {
                    appendDataToTable(data.rows.item(i));//获取某行数据的json对象
                }
            }
        }, function (ts, message) {
            alert(message);
            var tst = message;
        });
    });
}

function appendDataToTable(data) {//将数据展示到表格里面
    //uName,title,words
    var Id = data.sId;
    var Name = data.sName;
    var Sex = data.sSex;
    var Paper = data.sPaper;
    var strHtml = "";
    strHtml += "<tr>";
    strHtml += "<td>" + Id + "</td>";
    strHtml += "<td>" + Name + "</td>";
    strHtml += "<td>" + Sex + "</td>";
    strHtml += "<td>" + Paper + "</td>";
    strHtml += "</tr>";
    $("#tblData").append(strHtml);
}


function qudey() {
    //执行sql脚本，插入数据
  alert('qudey');
}
